/*
File:		PickUp.cpp
Author:		Ben Feldmann
Purpose:	Base class for game pickups
*/

#include "PickUp.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_Direct3D.h"
#include "SGD Wrappers/CSGD_XAudio2.h"
#include "EventSystem.h"
#include "MessageSystem.h"
#include "DestroyEntityMessage.h"
#include "GamePlayState.h"
#include "Game.h"

PickUp::PickUp()
{
	type = OBJ_PICKUP; 
	textTimer = 0.0f; 
	collided = false;

	// Get the pickup sound from gameplaystate
	sound = GamePlayState::GetInstance()->GetPickupSound();
	playedSound = false;
}

PickUp::~PickUp()
{
	if(GetTextureID() != -1 )
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(GetTextureID());
		SetTextureID(-1);
	}
	sound = -1;
}

bool PickUp::CheckCollision(IEntity* base) 
{
	if(Entity::CheckCollision(base) == true && base->GetType() == OBJ_PLAYER && collided == false)
	{
		// Send the "target_hit" event
		EventSystem::GetInstance()->SendEvent("pickup_hit", new int(itemType), new int(value),
			new int(dynamic_cast<Player*>(base)->GetPlayerNum()));
		collided = true;

		if(playedSound == false)
		{
			CSGD_XAudio2::GetInstance()->SFXPlaySound(sound);
			playedSound = true;
		}

		return true;
	}
	else
		return false;
}

void PickUp::Update(float elapsed)
{
	if(collided == true)
	{
		textTimer += elapsed;

		textY -= 50*elapsed;

		if(textTimer >= 3.0f)
		{
			DestroyEntityMessage* msg = new DestroyEntityMessage(this);
			MessageSystem::GetInstance()->SendMsg(msg);
			msg = nullptr;
		}
	}
	else
		textY = GetY();
}

void PickUp::Render()
{
	if(collided == false)
	{
		if(GetTextureID() != -1)
		{

			CSGD_TextureManager::GetInstance()->Draw(GetTextureID(), int(GetX() - GamePlayState::GetInstance()->GetCamX()), int(textY - GamePlayState::GetInstance()->GetCamY()));
		}
	}
	else
	{
		CSGD_Direct3D::GetInstance()->GetSprite()->Flush();
		if(itemType == METAL)
		{
			if(Game::GetInstance()->GetLanguage() == true)
			{
				TCHAR values[32];
				_stprintf_s(values, 32, _T("+%i Metal"), value);
				CSGD_Direct3D::GetInstance()->DrawTextW(values,(int)GetX() - GamePlayState::GetInstance()->GetCamX(),(int)textY - GamePlayState::GetInstance()->GetCamY(),255,255,255);	
			}
			else 
			{
				TCHAR values[32];
				_stprintf_s(values, 32, _T("+%i %s"), value, GamePlayState::GetInstance()->metalStr);
				CSGD_Direct3D::GetInstance()->DrawTextW(values,(int)GetX() - GamePlayState::GetInstance()->GetCamX(),(int)textY - GamePlayState::GetInstance()->GetCamY(),255,255,255);	
			}
		}
		if(itemType == CIRCUITRY)
		{
			if(Game::GetInstance()->GetLanguage() == true)
			{
				TCHAR values[32];
				_stprintf_s(values, 32, _T("+%i Circuitry"), value);
				CSGD_Direct3D::GetInstance()->DrawTextW(values,(int)GetX() - GamePlayState::GetInstance()->GetCamX(),(int)textY+20 - GamePlayState::GetInstance()->GetCamY(),255,255,255);
			}		
			else
			{
				TCHAR values[32];
				_stprintf_s(values, 32, _T("+%i %s"), value, GamePlayState::GetInstance()->circuitStr);
				CSGD_Direct3D::GetInstance()->DrawTextW(values,(int)GetX() - GamePlayState::GetInstance()->GetCamX(),(int)textY+20 - GamePlayState::GetInstance()->GetCamY(),255,255,255);	
				
			}
		}
	}
}
